import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common'
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'
import { definePermission, Perm } from '~/modules/admin/auth/decorators/permission.decorator'

import { CreatePlanTaskDto } from './dto/create-plan-task.dto'
import { UpdatePlanTaskDto } from './dto/update-plan-task.dto'
import { PlanTaskService } from './plan-task.service'

export const permissions = definePermission('study:plan', {
  LIST: 'list',
  CREATE: 'create',
  READ: 'read',
  UPDATE: 'update',
  DELETE: 'delete',
} as const)

@ApiTags('PlanTask - 计划任务')
@ApiSecurityAuth()
@Controller('admin/study/plan/plan-task')
export class PlanTaskController {
  constructor(private readonly planTaskService: PlanTaskService) {}

  @ApiOperation({ summary: '创建计划任务' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @Perm(permissions.CREATE)
  @Post()
  create(@Body() createPlanTaskDto: CreatePlanTaskDto) {
    return this.planTaskService.create(createPlanTaskDto)
  }

  @ApiOperation({ summary: '替换计划任务' })
  @ApiResponse({ status: 201, description: '替换成功' })
  @Post('replace')
  replace(@Body() createPlanTaskDto: CreatePlanTaskDto) {
    return this.planTaskService.replace(createPlanTaskDto)
  }

  @ApiOperation({ summary: '获取计划任务列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.LIST)
  @Get()
  findAll() {
    return this.planTaskService.findAll()
  }

  @ApiOperation({ summary: '获取计划任务详情' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.READ)
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.planTaskService.findOne(+id)
  }

  @ApiOperation({ summary: '更新计划任务' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Perm(permissions.UPDATE)
  @Put(':id')
  update(@Param('id') id: string, @Body() updatePlanTaskDto: UpdatePlanTaskDto) {
    return this.planTaskService.update(+id, updatePlanTaskDto)
  }

  @ApiOperation({ summary: '删除计划任务' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Perm(permissions.DELETE)
  remove(@Param('id') id: string) {
    return this.planTaskService.remove(+id)
  }
}
